package org.ilovecode.mvc
{
	import com.asual.SWFAddress;
	import com.asual.SWFAddressEvent;
	
	import flash.display.DisplayObject;
	import flash.errors.IllegalOperationError;
	
	import org.ilovecode.display.AboutApartments;
	import org.ilovecode.display.AboutBusiness;
	import org.ilovecode.display.AboutProjectApartments;
	import org.ilovecode.display.AboutProjectBusiness;
	import org.ilovecode.display.ApartmentLayout;
	import org.ilovecode.display.Conditions;
	import org.ilovecode.display.Contact;
	import org.ilovecode.display.ContactBusiness;
	import org.ilovecode.display.Discount;
	import org.ilovecode.display.FAQ;
	import org.ilovecode.display.FloorLayout;
	import org.ilovecode.display.FloorLayoutBusiness;
	import org.ilovecode.display.FloorLayoutBusinessSelling;
	import org.ilovecode.display.Gallery;
	import org.ilovecode.display.Garage;
	import org.ilovecode.display.ImageView;
	import org.ilovecode.display.Intro;
	import org.ilovecode.display.Render;
	import org.ilovecode.display.SearchApartment;
	import org.ilovecode.events.FaqEvent;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.Screen;
	import org.ilovecode.screen.ScreenGroup;
	import org.ilovecode.screen.ScreenName;
	import org.ilovecode.utils.method.capitalizeFirstLetter;

	public class Controller implements IController
	{
		private static var _instance:IController;
		private var model:IModel;
		private var _firstRun:Boolean = true;
		
		public function Controller(lock:Singleton)
		{
			if (!lock) throw new IllegalOperationError("Can't instantiate Singleton class, call getInstance method");
			
			model = Model.getInstance();
		}
		
		public static function getInstance():IController
		{
			if (!_instance) _instance = new Controller(new Singleton());
			
			return _instance;
		}
		
		public function getScreenXML(screenName:String, group:String):XML
		{
			var groupName:String = group.replace("screen group ", "").toLowerCase();
			var xmls:Array = model.screenXMLs;
			for (var i:int = 0; i < xmls.length; i++) 
			{
				if (XML(xmls[i]).@name == screenName && XML(xmls[i]).@group == groupName)
					return XML(xmls[i]);
			}
			
			return null;
		}
		
		public function switchFaqItem(id:int, offset:Number):void
		{
			var e:FaqEvent = new FaqEvent(FaqEvent.SWITCH_ITEM);
			e.id = id;
			e.offset = offset;
			model.dispatchEvent(e);
		}
		
		private function handle_SWFAddressExternalChange(event:SWFAddressEvent):void
		{
			var args:Array = SWFAddress.getValue().split("/");
			//Remove first arg which is empty string
			if (args[0] == "")
				args.splice(0, 1);

			try
			{
				var screenName:String = "screen name " + String(args[1]).toUpperCase();
				
				if (args.length >= 4 && (screenName == ScreenName.FLOOR_LAYOUT || screenName == ScreenName.APARTMENT_LAYOUT))
				{
					if (args[2] == "floor")
					{
						if (args[4] == "object")
						{
							var isBusiness:Boolean = (String(args[0]) == "business");
							showApartment(int(args[5]), int(args[3]), String(args[1]), isBusiness);
							return;
						}
						showFloor(int(args[3]));
						return;
					}
					/*else if (args[1] == "object")
					{
						showApartment(int(args[2]));
						return;
					}*/
				}
				
				if (args.length >= 3 && (screenName == ScreenName.LAYOUT_KINDERGARTEN || screenName == ScreenName.LAYOUT_CATERING || screenName == ScreenName.LAYOUT_SHOP))
				{
					if (args[2] == "object")
					{
						var isBusiness:Boolean = (String(args[0]) == "business");
						showApartment(int(args[3]), 0, "floorLayoutBusinessSelling", isBusiness);
						return;
					}
				}
				
				if (args.length >= 2)
					showScreen(screenName);
				else
					showScreen(ScreenName.INTRO);
			}
			catch (e:Error)
			{
				showScreen(ScreenName.INTRO);
			}
		}
		
		private function handle_SWFAddressChange(event:SWFAddressEvent):void
		{
			if (!model.currentScreen)
				return;
			
			var pageName:String = ScreenName.getLabel(model.currentScreen.type);
			var outputPageName:String = (pageName == "") ? "Početna" : pageName;
			SWFAddress.setTitle(outputPageName + " - Centar Bundek");
		}
		
		public function showScreen(screenName:String):void
		{
			if (_firstRun) 
			{
				_firstRun = false;
				SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, handle_SWFAddressExternalChange);
				SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handle_SWFAddressChange);
				return;
			}
			
			var page:String = screenName.replace("screen name ", "").toLowerCase();
			switch(screenName)
			{
				case "":
					//Default page
				case ScreenName.RENDER:
					model.currentScreen = new Render(model.group);
					break;
				case ScreenName.INTRO:
					model.currentScreen = new Intro();
					break;
				case ScreenName.ABOUT:
					model.currentScreen = (model.group == ScreenGroup.APARTMENTS) ? new AboutApartments() : new AboutBusiness();
					break;
				case ScreenName.ABOUT_PROJECT:
					model.currentScreen = (model.group == ScreenGroup.APARTMENTS) ? new AboutProjectApartments() : new AboutProjectBusiness();
					break;
				case ScreenName.GALLERY:
					model.currentScreen = new Gallery();
					break;
				case ScreenName.GARAGE:
					model.currentScreen = new Garage();
					break;
				case ScreenName.DISCOUNT:
					model.currentScreen = new Discount();
					break;
				case ScreenName.CONDITIONS:
					model.currentScreen = new Conditions();
					break;
				case ScreenName.LAYOUT_RENT:
					model.currentScreen = new FloorLayoutBusiness();
					break;
				case ScreenName.LAYOUT_SHOP:
					model.currentScreen = new FloorLayoutBusinessSelling("shop");
					break;
				case ScreenName.LAYOUT_CATERING:
					model.currentScreen = new FloorLayoutBusinessSelling("catering");
					break;
				case ScreenName.LAYOUT_KINDERGARTEN:
					model.currentScreen = new FloorLayoutBusinessSelling("kindergarten");
					break;
				case ScreenName.SEARCH:
					model.currentScreen = new SearchApartment();
					break;
				case ScreenName.FAQ:
					model.currentScreen = new FAQ();
					break;
				case ScreenName.CONTACT:
					model.currentScreen = (model.group == ScreenGroup.APARTMENTS) ? new Contact() : new ContactBusiness();
					break;
				case ScreenName.FLOOR_LAYOUT:
					model.currentScreen = (model.group == ScreenGroup.APARTMENTS) ? new FloorLayout() : new FloorLayoutBusiness();
					break;
				/*case ScreenName.APARTMENT_LAYOUT:
					break;*/
				default:
					throw new Error("Illegal screen");
					break;
			}
			if (!model.group)
				SWFAddress.setValue("/");
			else
			{
				var group:String = (model.group == ScreenGroup.APARTMENTS) ? "apartments" : "business";
				SWFAddress.setValue("/" + group + "/" + page);
			}
		}
		
		public function showFloor(floorNumber:int):void
		{
			//Determine screen to be displayed
			var screen:String = (floorNumber <= -1) ? ScreenName.GARAGE : ScreenName.FLOOR_LAYOUT;
			
			showScreen(screen);
			FloorScreen(model.currentScreen).showFloor(floorNumber);
			
			SWFAddress.setValue(SWFAddress.getValue() + "/floor/" + floorNumber);
		}
		
		public function showApartment(roomID:int, floorID:int, source:String, isBusiness:Boolean = false):void
		{
			model.currentScreen = new ApartmentLayout(roomID, floorID, source, isBusiness);
			
			SWFAddress.setValue(SWFAddress.getValue() + "/object/" + roomID);
		}
		
		public function showImage(image:DisplayObject):void
		{
			model.currentScreen = new ImageView(image, model.currentScreen);
		}
		
		public function setCurrentScreen(screen:Screen):void
		{
			model.currentScreen = screen;
		}
	}
}

//Private class for Singleon pattern
class Singleton { public function Singleton() {} }